package com.exp.bookhouse.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exp.bookhouse.pojo.Category;
import com.exp.bookhouse.service.CategoryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Api(value = "图书分类管理接口",tags = "管理员管理图书分类操作")
@Controller
@RequestMapping("/admin")
public class CategoryControllerAdmin {


    @Autowired
    private CategoryService categoryService;


    @ApiOperation(value = "查询分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "p", value = "页数", required = false, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "s", value = "显示记录数", required = false, dataType = "Int")
    })
    @GetMapping("/toCategory")
    public String toCategory(@RequestParam(value = "p", required = false, defaultValue = "1")Integer page,
                             @RequestParam(value = "s", required = false, defaultValue = "5")Integer size,
                             Model model){
        IPage<Category> iPage = categoryService.queryCategoryByPage(page,size);
        model.addAttribute("iPage",iPage);
        return "category";
    }


    @ApiOperation(value = "新增分类")
    @PostMapping("/saveCategory")
    public String saveCategory(@ApiParam(value = "分类信息",required = true) Category category){
        categoryService.saveCategory(category);
        return "redirect:/admin/toCategory";
    }


    @GetMapping("/toSaveCategory")
    public String toSaveCategory(){
        return "categoryAdd";
    }

    @ApiOperation(value = "根据id查询分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "分类id", required = true, dataType = "Long")
    })
    @GetMapping("/toUpdateCategory/{id}")
    public String toUpdateCategory(@PathVariable("id")Long id, Model model){
        Category category = categoryService.queryCategoryById(id);
        model.addAttribute("category",category);
        return "categoryUpdate";
    }

    @ApiOperation(value = "更新分类")
    @PostMapping("/updateCategory")
    public String updateCategory(@ApiParam(value = "分类信息",required = true) Category category){
        int count = categoryService.updateCategory(category);
        return "redirect:/admin/toCategory";
    }

    @ApiOperation(value = "删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "编号id", required = true, dataType = "Long")
    })
    @GetMapping("/deleteCategory/{id}")
    public String deleteCategory(@PathVariable("id")Long id) throws Exception {
        categoryService.deleteCategory(id);
        return "redirect:/admin/toCategory";
    }

}
